package cn.droidlover.mywegit.util;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.util.Map;

public class WeatheData extends ViewModel {



    private MutableLiveData<String> weather;
    private MutableLiveData<String> temp;
    private MutableLiveData<String> weather_pic;
    private MutableLiveData<String> date;



  //  private  MutableLiveData<String>


    public MutableLiveData<String> getTemp() {
        return temp;
    }
    public void setTemp(MutableLiveData<String> temp) {
        this.temp = temp;
    }

    public MutableLiveData<String> getDate() {
        return date;
    }

    public MutableLiveData<String> getWeather_pic() {
        return weather_pic;
    }

    public WeatheData (){


       if(weather ==null)
       {
           weather =new MutableLiveData<>();
           weather.postValue("多云");
       }

       if(temp==null)
       {
           temp=new MutableLiveData<>();
           temp.postValue("null/null");

       }


       if(weather_pic==null){

           weather_pic=new MutableLiveData<>();
           weather_pic.postValue("http://app1.showapi.com/weather/icon/day/07.png");
       }

       if(date==null)
       {
           date=new MutableLiveData<>();
           date.postValue("20210126");

       }
   }

    public MutableLiveData<String> getWeather() {

        return weather;
    }

    public void setResult(String jsonStr)
    {


        Map<String, Object> jsMap = FastJsonUtils.changeJsonToMap(jsonStr);

        String w1=jsMap.get("day_weather").toString();
        String w2=jsMap.get("night_weather").toString();
        if(w1.equals(w2))
             this.weather.postValue(w1);
        else
            this.weather.postValue(w1+"转"+w2);


        this.temp.postValue(jsMap.get("night_air_temperature").toString()
                +"/"
                +jsMap.get("day_air_temperature").toString()
                +"℃");


        this.weather_pic.postValue(jsMap.get("day_weather_pic").toString());

        String T=jsMap.get("day").toString();
        if(T!=null){
        if(T.length()>6)
          T=T.substring(0,4)+"-"+T.substring(4,6)+"-"+T.substring(6);
        this.date.postValue(T);}

    }



}
